package org.rency.common.utils.tool;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import org.rency.common.utils.exception.CoreException;
import org.springframework.util.Assert;

/**
 * 注解工具类
 * @author: user_rcy@163.com
 * <p>Created on 2015年11月4日</p>
 * @version: V1.0.0
 * @since JDK 1.8
 */
public class ReflectUtils {

	/**
	 *  获取注解实例
	 * @param execClass
	 * @param execMethod
	 * @param annotationClass
	 * @return
	 * @throws CoreException
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static Object getAnnotationValue(Class execClass,Method execMethod,Class annotationClass) throws CoreException{
		Assert.isTrue((execClass == null && execMethod == null),"参数不能为空");
		if(execMethod != null){
			return execMethod.getAnnotation(annotationClass);
		}else{
			return execClass.getAnnotation(annotationClass);
		}
	}
	
	/**
	 * 根据类、方法名获取方法实体
	 * @param actionClass
	 * @param methodName
	 * @return
	 * @throws NoSuchMethodException
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static Method getActionMethod(Class actionClass, String methodName)throws NoSuchMethodException {
		Method method;
		try {
			method = actionClass.getMethod(methodName, new Class[0]);
		} catch (NoSuchMethodException e) {
			throw e;
		}
		return method;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static Annotation getMethodAnnotation(Method method, Class annotationClass) {
        return method.getAnnotation(annotationClass);
    }
	
}